package org.squirrel.modules.bigscreen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.entity.DataSource;
import org.squirrel.modules.bigscreen.param.ConnectionParam;
import org.squirrel.modules.bigscreen.service.DataSourceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Api(tags = "数据源管理")
@RequestMapping("/dataSource")
public class DataSourceController extends SquirrelController<DataSource, DataSourceService> {

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 获取所有数据源
     * @return
     */
    @ApiOperation(value = "获取所有数据源", notes = "获取所有数据源")
    @GetMapping("/queryAllDataSource")
    public Result<?> queryAllDataSource() {
    	try {
    		List<DataSource> dataSourceLts = dataSourceService.queryAllDataSource();
    		return Result.OK("获取所有数据源", dataSourceLts);
    	}catch(Exception e) {
    		log.error("获取所有数据源异常", e);
			return Result.error("获取所有数据源失败,原因:" + e.getMessage());
    	}
    }

    /**
     * 测试 连接
     * @param connectionParam
     * @return
     */
    @ApiOperation(value = "测试连接", notes = "测试连接")
    @PostMapping("/testConnection")
    public Result<?> testConnection(@Validated @RequestBody ConnectionParam connectionParam) {
    	try {
    		Boolean testRes = dataSourceService.testConnection(connectionParam);
    		return Result.OK("测试连接", testRes);
    	}catch(Exception e) {
    		log.error("测试连接异常", e);
			return Result.error("测试连接失败,原因:" + e.getMessage());
    	}
    }

}
